//
//  mobanController.m
//  pacc
//
//  Created by 测试 on 2018/7/30.
//  Copyright © 2018年 测试. All rights reserved.
//

#import "mobanController.h"
#import "mobanCell.h"
#import "mobanModel.h"
#import "mubanView.h"
#import "taskDetailsCVc.h"
#import "shenhetuihuiController.h"
@interface mobanController ()<UITableViewDelegate,UITableViewDataSource,UISearchResultsUpdating,UISearchBarDelegate>
@property(nonatomic,strong)UITableView * mytableView;
@property(nonatomic,copy)NSMutableArray * dataSource;
@property(nonatomic,copy)NSMutableArray * temdataSource;
@property(nonatomic,strong)UIView * maskLayer;
@property(nonatomic,strong)mubanView * mubanview;
@property (nonatomic, strong) UISearchController *searchController;

@end

@implementation mobanController

- (void)viewDidLoad {
    [super viewDidLoad];

    UISearchController *search = [[UISearchController alloc]initWithSearchResultsController:nil];
    // 设置结果更新代理
    self.searchController = search;
    //    search.searchBar.delegate = self;
    search.searchBar.placeholder = @"搜索";
    self.definesPresentationContext = YES;
    [_searchController.searchBar setValue:@"取消" forKey:@"_cancelButtonText"];
    search.searchResultsUpdater = self;
    // 因为在当前控制器展示结果, 所以不需要这个透明视图
    search.dimsBackgroundDuringPresentation = NO;
    
    self.mytableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.mytableView.tableHeaderView = search.searchBar;
    [self.view addSubview:_mytableView];
    _mytableView.delegate = self;
    _mytableView.dataSource = self;
    [_mytableView registerNib:[UINib nibWithNibName:@"mobanCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    if (!_dataSource) {
        _dataSource = [[NSMutableArray alloc]init];
    } if (!_temdataSource) {
        _temdataSource = [[NSMutableArray alloc]init];
    }
    for (NSDictionary *childListArr in _mubanArr) {
        mobanModel * model = [mobanModel new];
        [model setValuesForKeysWithDictionary:childListArr];
        [_dataSource addObject:model];
        [_temdataSource addObject:model];
    }

    _mubanview = [[mubanView alloc]initWithFrame:CGRectMake(50, 100, ScreenWidth-100, ScreenHeight-216)];
    _mubanview.hidden = YES;
    [self.view addSubview:_mubanview];
    
}
- (void)action_removeMask{
    // 移除全屏点击事件
    [_maskLayer removeFromSuperview];
    _mubanview.hidden = YES;
}
- (void)addMaskLayer{
    // 添加遮罩
    _maskLayer = [UIView  new];
    [_maskLayer setFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
    [_maskLayer setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.3]];
    [self.view addSubview:_maskLayer];
}
#pragma mark search
-(void)updateSearchResultsForSearchController:(UISearchController *)searchController{
    NSString * str = searchController.searchBar.text;
    NSString *temStr;
    NSMutableArray *temArr = [[NSMutableArray alloc]init];
    for (int i = 0; i<_temdataSource.count; i++) {
        mobanModel * model = _temdataSource[i];
        temStr = model.levelName;
        if ([temStr containsString:str]) {
            [temArr addObject:_temdataSource[i]];
        }
    }
    if ([str isEqualToString:@""]) {
        _dataSource = [_temdataSource mutableCopy];
    }else{
        _dataSource = [temArr mutableCopy];
    }
     [self.mytableView reloadData];
}
#pragma mark tableview datasource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataSource.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    mobanCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.model = _dataSource[indexPath.row];
    return cell;
}
#pragma mark tableview delegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    mobanCell * cell = [tableView cellForRowAtIndexPath:indexPath];
    if ([cell.hasChild isEqualToString:@"1"]) {
        mobanController *vc = [mobanController new];
        vc.zhenduanyijianStr = self.zhenduanyijianStr;
        vc.jianchasuojianStr = self.jianchasuojianStr;
        vc.mubanArr = cell.childList;
        [self.navigationController pushViewController:vc animated:YES];
    }else{
//        NSLog(@"没有下一级模板");
        [self addMaskLayer];
        _mubanview.jianchasuojianTV.text = cell.templateDesc;
        _mubanview.zhenduanyijianTV.text = cell.diagnosisInitial;
        _mubanview.hidden = NO;
        [_mubanview.backBtn addTarget:self action:@selector(backXq) forControlEvents:UIControlEventTouchUpInside];
        [_mubanview.yingyongBtn addTarget:self action:@selector(yingyongClick) forControlEvents:UIControlEventTouchUpInside];
        [_mubanview.zhuijiaBtn addTarget:self action:@selector(zhuijiaClick) forControlEvents:UIControlEventTouchUpInside];
        [self.view bringSubviewToFront:_mubanview];
    }
}
#pragma mark click
-(void)backXq{
    for (UIViewController *controller in self.navigationController.viewControllers) {
        if ([controller isKindOfClass:[taskDetailsCVc class]]) {
            [self.navigationController popToViewController:controller animated:YES];
        }
        if ([controller isKindOfClass:[shenhetuihuiController class]]) {
            [self.navigationController popToViewController:controller animated:YES];
        }
    }
    
}
-(void)yingyongClick{
    my_AppDelegate.backXQ = YES;
    my_AppDelegate.zhenduanyijianStr = _mubanview.zhenduanyijianTV.text;
    my_AppDelegate.jianchasuojianStr = _mubanview.jianchasuojianTV.text;
    for (UIViewController *controller in self.navigationController.viewControllers) {
        if ([controller isKindOfClass:[taskDetailsCVc class]]) {
            [self.navigationController popToViewController:controller animated:YES];
        }
        if ([controller isKindOfClass:[shenhetuihuiController class]]) {
            [self.navigationController popToViewController:controller animated:YES];
        }
    }
    
    
}
-(void)zhuijiaClick{
    my_AppDelegate.backXQ = YES;
    my_AppDelegate.zhenduanyijianStr = [_zhenduanyijianStr stringByAppendingString: _mubanview.zhenduanyijianTV.text];
    my_AppDelegate.jianchasuojianStr = [_jianchasuojianStr stringByAppendingString: _mubanview.jianchasuojianTV.text];
    for (UIViewController *controller in self.navigationController.viewControllers) {
        if ([controller isKindOfClass:[taskDetailsCVc class]]) {
            [self.navigationController popToViewController:controller animated:YES];
        }
        if ([controller isKindOfClass:[shenhetuihuiController class]]) {
            [self.navigationController popToViewController:controller animated:YES];
        }
    }
}
#pragma mark other
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self action_removeMask];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
